/**
 * Created by zhu on 2016/1/30.
 */
$(function(){
    var myhregf = window.location.href;
    var count = myhregf.split('/').length;
    var id = myhregf.split('/')[count-1];


    $.get(contextPath + "/goods/"+id, function (data) {
        initGoods(data);
    });
    $.get(contextPath + "/fans/address/flag/true", function (data) {
        initAddress(data);
    });

});
function initGoods(data){
    data = jQuery.parseJSON(data);
    $('#goods_introduce').empty();
    var temp = '   <div class="item-goods-img">'+
        '<img src="'+contextPath+data.bean.index_url+'">'+
    '</div>'+
    '<div class="item-goods-text">'+
    '<div class = "introduce">'+data.bean.name+'</div>'+
    '<div class = "price">'+
    '<div class = "this-price">价格:￥'+data.bean.price+'</div>'+
    '</div>'+
        '<input type="hidden" id="goodsId" name="goodsId" value="'+data.bean.id+'"/>'+
    '</div>';
    $('#goods_introduce').append(temp);
    initPrice(data.bean.price);
}

function initAddress(data){
    data = jQuery.parseJSON(data);
    $('#myadress').empty();
    $.each(data.list,function(i,item){
        if(i == 0){
            var temp = ' <div class="infoOfConsignee" onclick="address()">'+
                '    <div class="col01-15 colfloat">'+
            ' <span class="icon icon-search"></span>'+
            ' </div>'+
            ' <div class="col03-70 colfloat">'+
            ' <div class = "inner_info info_01">'+
            ' <div class = "inner_detail_left">'+item.fansName+'</div>'+
            ' <div class = "inner_detail_right">'+item.phone+'</div>'+
            '</div>'+
            '<div class = "inner_info info_02">'+item.fansAddress+'</div>'+
            '</div>'+
            '<div class="col02-15 colfloat">'+
            '<input type="hidden" id="addressId" name="addressId" value="'+item.id+'"/>'+
            '<span class="icon icon-right"></span>'+
            '</div>'+
            '</div>';
            $('#myadress').append(temp);
        }
    });

}

function initPrice(price){
    var number = $('#number');
    var titlePrice = $('.title_price');
    var titleNumber = $('#title_number');
    number.text(1);
    titleNumber.text(1);
    titlePrice.text(price);

    $('#add').on('click',function(){
        var nowNumber = Number(number.text())+1;
        number.text(nowNumber);
        titleNumber.text(nowNumber);
        titlePrice.text(nowNumber*price);

    });

    $('#reduce').on('click',function(){
        if(Number(number.text())>1){
        var nowNumber = Number(number.text())-1;
        number.text(nowNumber);
        titleNumber.text(nowNumber);
        titlePrice.text(nowNumber*price);
        }
    });

    $('#order_push').on('click',function(){
        var addressId =  $('#addressId').val();
        var goodsId =  $('#goodsId').val();
        var titlePrice = $($('.title_price')[0]).text();
        var titleNumber = $('#title_number').text();
        var note = $('#note').val();
        $.post(contextPath+"/order/purchase",{goodsId0:goodsId,
            price0:titlePrice,addressId:addressId,amount0:titleNumber,note:note,flag:1},function(data){
            if(data.status == 0){
                window.location.href=contextPath+"/cart/paymentMode/"+data.text;
            }else{
                alert("请添加收货地址！");
                window.location.href= contextPath+"/my/add_address";
            }
        },'json')

    })
}